<template>
  <div class="pb30">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
      @submit.native.prevent
    >
      <!-- <el-form-item label="群组名称" prop="groupNames">
        <el-input
          v-model="queryParams.groupNames"
          placeholder="请输入群组名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item> -->

      <el-form-item>
        <el-button
          size="mini"
          type="success"
          @click="handleAdd"
          v-hasPermi="['system:sendRecord:add']"
        >
          新增群发
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column
        label="操作"
        align="center"
        width="130"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <span class="blue cp" @click="handleView(scope.row)">
            <i class="el-icon-view"></i> 查看
          </span>
        </template>
      </el-table-column>
      <el-table-column label="群组名称" align="center" prop="groupNameList">
        <template slot-scope="scope">
          <span
            v-for="(item, index) in scope.row.groupNameList"
            :key="index"
            class="mb10 fl"
          >
            <template v-if="index === 0">{{ item }}</template>
            <template v-else>、{{ item }}</template>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="发送内容"
        align="center"
        prop="sendInfo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="发送状态"
        align="center"
        prop="status"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status === -1" class="orange">发送中</span>
          <span v-if="scope.row.status === 0" class="green">成功</span>
          <span v-if="scope.row.status === 1" class="red">异常</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发送人"
        align="center"
        prop="createBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="发送时间"
        align="center"
        prop="createTime"
        width="200"
        :show-overflow-tooltip="true"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加修改 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="群组名称" prop="groupId">
          <el-select
            v-model="form.groupId"
            placeholder="请选择群组名称"
            clearable
            filterable
            class="w100"
            @change="selectGroup"
          >
            <el-option
              v-for="(item, index) in groupOption"
              :key="index"
              :label="item.name"
              :value="item.groupId"
            />
          </el-select>
          <div
            v-if="form.groupNameList && form.groupNameList.length != 0"
            class="mb10 w100"
          ></div>
          <el-tag
            v-for="(item, index) in form.groupNameList"
            :key="index"
            class="mr10 mb10"
          >
            {{ item }}
          </el-tag>
        </el-form-item>
        <el-form-item label="房间号" prop="room">
          <el-select
            v-model="form.room"
            placeholder="请选择房间号"
            clearable
            filterable
            class="w100"
          >
            <el-option
              v-for="(item, index) in roomOption"
              :key="index"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="缺人情况" prop="people">
          <el-select
            v-model="form.people"
            placeholder="请选择"
            clearable
            class="w100"
            filterable
          >
            <el-option
              v-for="item in dict.type.sys_hands_status"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="打法" prop="play">
          <el-select
            v-model="form.play"
            placeholder="请选择"
            clearable
            class="w100"
            filterable
          >
            <el-option
              v-for="item in dict.type.sys_game_play"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Kd要求" prop="kd">
          <el-select
            v-model="form.kd"
            placeholder="请选择"
            clearable
            class="w100"
            filterable
          >
            <el-option
              v-for="item in dict.type.sys_game_kd"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="等级要求" prop="level">
          <el-select
            v-model="form.level"
            placeholder="请选择"
            clearable
            class="w100"
            filterable
          >
            <el-option
              v-for="item in dict.type.sys_game_level"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="其他说明" prop="info">
          <el-input
            v-model="form.info"
            placeholder="请输入内容，1~10个字符"
            maxlength="10"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          size="small"
          :loading="btnLoading"
          @click="submitForm"
        >
          发 送
        </el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog
      title="群发详情"
      :visible.sync="detailOpen"
      width="600px"
      append-to-body
    >
      <detailDialog ref="detailInfo" />
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="detailOpen = false">
          关 闭
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sendList, sendInfo, addSend, getGroupName } from "@/api/project/send";
import { groupList, groupInfo } from "@/api/project/group";

import detailDialog from "@/components/Aproject/detail/group";
export default {
  components: {
    detailDialog,
  },
  dicts: ["sys_hands_status", "sys_game_play", "sys_game_kd", "sys_game_level"],
  data() {
    return {
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupNames: undefined,
      },
      total: 0,
      list: [],

      title: "",
      open: false,

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        groupId: [
          { required: true, message: "请选择群组名称", trigger: "change" },
        ],
        room: [{ required: true, message: "请选择房间号", trigger: "change" }],
        people: [
          { required: true, message: "请选择缺人情况", trigger: "change" },
        ],
        play: [{ required: true, message: "请选择打法", trigger: "change" }],
        kd: [{ required: true, message: "请选择kd要求", trigger: "change" }],
        level: [
          { required: true, message: "请选择等级要求", trigger: "change" },
        ],
        info: [{ validator: this.voidInfo, trigger: "blur" }],
      },

      groupOption: [],
      roomOption: [],
      roomNum: 300,
      detailOpen: false,
      btnLoading: false,
    };
  },

  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      sendList(this.queryParams)
        .then((res) => {
          this.list = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 获取群组名称
    getGroup() {
      groupList().then((res) => {
        this.groupOption = res.rows;
      });
    },

    selectGroup(e) {
      this.form.groupNameList = [];
      if (e != null && e != "") {
        groupInfo(e).then((res) => {
          this.form.groupNameList = JSON.parse(res.data.wxGroups);
        });
      }
    },

    // 创建房间号
    createRoom() {
      var arr = [];
      for (let index = 0; index < this.roomNum; index++) {
        arr.push({
          name: "yy117，" + [index + 1] + "房",
        });
      }
      this.roomOption = arr;
    },

    // 校验不能输入数字
    voidInfo(rule, value, callback) {
      if (value && /\d/.test(value)) {
        callback(new Error("不能输入数字"));
      } else {
        callback();
      }
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        groupNameList: [],
        groupId: undefined,
        room: undefined,
        people: undefined,
        play: undefined,
        kd: undefined,
        level: undefined,
        info: undefined,
        sendInfo: undefined,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.createRoom();
      this.getGroup();
      this.open = true;
      this.title = "新增群发";
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          var other = "";
          if (this.form.info != undefined) {
            other = "，" + this.form.info;
          } else {
            other = "";
          }
          var cont =
            this.form.room +
            "，" +
            this.form.people +
            "，" +
            this.form.play +
            "，" +
            this.form.kd +
            "，" +
            this.form.level +
            other;

          var param = {
            groupNameList: this.form.groupNameList,
            sendInfo: cont,
          };
          addSend(param)
            .then((res) => {
              if (res.code === 200) {
                this.$modal.msgSuccess("操作成功");
              } else {
                this.$modal.msgError(res.msg);
              }
              this.open = false;
              this.getList();
              this.btnLoading = false;
            })
            .finally(() => {
              this.btnLoading = false;
            });
        }
      });
    },

    // 查看详情
    handleView(row) {
      sendInfo(row.sendId).then((res) => {
        this.detailOpen = true;
        this.$nextTick(() => {
          this.$refs.detailInfo.detailForm = res.data;
        });
      });
    },
  },
};
</script>

<style>
.fl {
  float: left;
}
.fr {
  float: right;
}
.w100 {
  width: 100%;
}

.el-dialog__header {
  border-bottom: 1px solid #ddd;
}
.blue {
  color: #1890ff;
}
.mr10 {
  margin-right: 10px;
}
.mb10 {
  margin-bottom: 10px;
}
.pb30 {
  padding-bottom: 30px;
}
.cp {
  cursor: pointer;
}

.green {
  color: #13ce66;
}

.orange {
  color: orange;
}
.red {
  color: red;
}
</style>
